1.利用C++ 11 新包含的chrono库:1
2
3
4
5
6
7
8#include<chrono>
std::chrono::high_resolution_clock::time_point beg, end;
std::chrono::duration<double> time_span;
beg = std::chrono::high_resolution_clock::now();
// do something
end = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(end - beg);
std::cout << "time: " << time_span.count() * 1000 << "ms\n";
2.使用time库:1
2
3
4
clock_t beg = clock();
// do something
std::cout << "time: " << (double)( clock() - beg ) / CLOCKS_PER_SEC * 1000 << "ms\n";